/*
 * @Author: your name
 * @Date: 2021-12-15 07:33:23
 * @LastEditTime: 2021-12-16 15:03:27
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \day7\my-app\src\store\index.js
 */
import Vue from 'vue'
import Vuex from 'vuex'

import axios from "../api/axios"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list: [],
    shopList: [],
    allc: false
  },
  mutations: {},
  actions: {
    getList(vuex) {
      if (!vuex.state.list.length) {
        axios.get("/api/list").then(res => {
          vuex.state.list = res.data
        })
      }
    },
    // 数量++
    addNum(vuex, obj) {
      vuex.state.list.forEach(item => {
        item.child.forEach(v => {
          if (v.id == obj.id) {
            v.count++
          }
        })
      })
    },
    // 数量--
    redNum(vuex, obj) {
      vuex.state.list.forEach(item => {
        item.child.forEach(v => {
          if (v.id == obj.id) {
            v.count--
          }
        })
      })
    },
    // 筛选出来数量大于1的
    getShoplist(vuex) {
      vuex.state.list.forEach(item => {
        item.child.forEach(v => {
          if (v.count > 0) {
            vuex.state.shopList.push(v)
          }
        })
      })
    },
    // 全选
    AllChecks(vuex,check) {
      vuex.state.shopList.forEach(item => {
        item.flag = check
      })
    },
    // 反选
    ItemChecks(vuex) {
      vuex.state.allc=vuex.state.shopList.every(item => {
          // item.flag=!item.flag
          return item.flag
      })
      return vuex.state.allc
    },
  },
})